<%@page import="cn.sendto.service.CarInfoService"%>
<%@page import="cn.sendto.model.CarInfoModel"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");

	String carnum = request.getParameter("carnum");
	String cartype = request.getParameter("cartype");
	String color = request.getParameter("color");
	String residentid = request.getParameter("residentid");
	String drivenum = request.getParameter("drivenum");
	String parkid = request.getParameter("parkid");

	//验证数据的有效性
	ArrayList<Integer> errlist = new ArrayList<Integer>();
	HashMap<String, String> formlist = new HashMap<String, String>();
	if (carnum == null || carnum.equals("")) {
		errlist.add(1);
	} else {
		formlist.put("carnum", carnum);
	}
	if (cartype == null || cartype.equals("")) {
		errlist.add(2);
	} else {
		formlist.put("cartype", cartype);
	}

	if (color == null || color.equals("")) {
		errlist.add(3);
	} else {
		formlist.put("color", color);
	}
	if (drivenum == null || drivenum.equals("")) {
		errlist.add(4);
	} else {
		formlist.put("drivenum", drivenum);
	}
	
	if (errlist.size() > 0) {
		request.setAttribute("info", errlist);
		request.setAttribute("form", formlist);
		request.getRequestDispatcher("addcar.jsp").forward(request, response);
		return;
	}

	CarInfoModel cim = new CarInfoModel();
	cim.setCarNum(carnum);
	cim.setCarType(cartype);
	cim.setColor(color);
	cim.setDriveNum(drivenum);
	cim.setParkId(Integer.parseInt(parkid));
	cim.setResidentId(Integer.parseInt(residentid));
	
	CarInfoService cis = new CarInfoService();
	 int rows = cis.add(cim);
	   if(rows > 0){
		    //返回注册页面
		   	 request.setAttribute("addcarinfo", "<span style='color:red;'>"+"添加成功"+"</span>"+"，可以继续添加。或者"+"<a href='carlist.jsp'>点击此处返回车辆列表</a>");
		   	 request.getRequestDispatcher("addcar.jsp").forward(request, response);
		   	 return;
		    }else if(rows == -1){
		    	 request.setAttribute("addcarinfo", "添加失败,车辆已存在");
		    	 request.setAttribute("form", formlist);
		       	 request.getRequestDispatcher("addcar.jsp").forward(request, response);
		       	 return;
		    }
		    else{
		   	 request.setAttribute("addcarinfo", "添加失败。");
		   	 request.getRequestDispatcher("addcar.jsp").forward(request, response);
		   	 return;
		    }
	%>